﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.ComponentModel;
using Org.Eurekaa.DNJ.UI.Common;
using System.Web.UI.HtmlControls;
using Org.Eurekaa.DNJ.Helpers;
using System.Drawing;
using Org.Eurekaa.DNJ.Collections;

namespace Org.Eurekaa.DNJ.UI.WebControls
{
    [ParseChildren(true, "Themes")]
    [PersistChildren(false)]
    [ToolboxBitmap(typeof(Org.Eurekaa.DNJ.UI.Resources.icons.DNJUIIcons), "DNJThemesWidget.bmp")]
    public class DNJThemesWidget : PlaceHolder
    {
        private DNJGenericCollection<DNJTheme> _themesList;
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [MergableProperty(false)]
        public DNJGenericCollection<DNJTheme> Themes
        {
            get
            {
                if (_themesList == null)
                {
                    _themesList = new DNJGenericCollection<DNJTheme>();
                }
                return _themesList;
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (DesignMode)
            {
                writer.Write("<a href=\"#\" style=\"");
                writer.Write("border: 1px solid rgb(204, 204, 204); padding: 3px 3px 3px 8px; background: rgb(238, 238, 238) repeat-x scroll 50% 50%; font-family: Trebuchet MS,Verdana,sans-serif; font-size: 11px; color: rgb(102, 102, 102); -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous; -moz-border-radius-topleft: 6px; -moz-border-radius-topright: 6px; -moz-border-radius-bottomright: 6px; -moz-border-radius-bottomleft: 6px; text-decoration: none; width: 139px; display: block; height: 14px; outline-color: -moz-use-text-color; outline-style: none; outline-width: 0pt; cursor: pointer;");
                writer.Write("<\">Theme Switcher</a>");
            }
            else
            base.Render(writer);
        }
        protected override void CreateChildControls()
        {
            //base.CreateChildControls();
            Controls.Clear();           
            HtmlGenericControl switcherdiv = new HtmlGenericControl("div");
            switcherdiv.Attributes.Add("id", "switcher_" + ClientID);
            Controls.Add(switcherdiv);

            HtmlGenericControl ul = new HtmlGenericControl("ul");
            
            foreach (DNJTheme theme in Themes)
            {
                HtmlGenericControl li = new HtmlGenericControl("li");
                HtmlAnchor cssAnchor = new HtmlAnchor();
                HtmlImage thumbnail = new HtmlImage();
                HtmlGenericControl title = new HtmlGenericControl("span");
                
                thumbnail.Src = theme.Thumbnail;
                title.InnerText = theme.Title;
                cssAnchor.HRef = theme.CssUrl;
                cssAnchor.Controls.Add(thumbnail);
                cssAnchor.Controls.Add(title);

                li.Controls.Add(theme);
                ul.Controls.Add(li);
            }


            if (ul.Controls.Count != 0)
            {                
                HtmlGenericControl maindiv = new HtmlGenericControl("div");
                maindiv.Attributes.Add("id", this.ClientID);
                maindiv.Attributes.Add("class", "jquery-ui-themeswitcher");
                HtmlGenericControl innerdiv = new HtmlGenericControl("div");

                innerdiv.Controls.Add(ul);
                maindiv.Controls.Add(innerdiv);
                Controls.Add(maindiv);
            }
            
            PageHelper.AppendJQueryPlugin("#switcher_" + ClientID, "themeswitcher", "{switcherpaneSelector : '#" + this.ClientID + "'}", this.Page.Header);
        }
    }
}
